package com.msmall.community.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.util.Constants;
import com.msmall.community.ApplicationManager;
import com.msmall.community.wiki.service.WikiManager;

public class WikiTag extends TagSupport{
	
	private static final long serialVersionUID = -2546013797744247966L;
	private static final Log logger = LogFactory.getLog(WikiTag.class);
	
	private String action;
	
	private String id;

	public int doStartTag() throws JspException {
		return (SKIP_BODY);
	}

	public int doEndTag() throws JspException {
		JspWriter writer = pageContext.getOut();
		String message = "";
		try {
			if("remove".equalsIgnoreCase(action)){
				if(isAuthorizedRemove()){
					message = "true";
				}else{
					message = "false";
				}
			}else if("update".equalsIgnoreCase(action)){
				if(isAuthorizedUpdate()){
					message = "true";
				}else{
					message = "false";
				}
			}else if("create".equalsIgnoreCase(action)){
				if(isAuthorizedCreate()){
					message = "true";
				}else{
					message = "false";
				}
			}else if("commend".equalsIgnoreCase(action)){
				if(isAuthorizedCommend()){
					message = "true";
				}else{
					message = "false";
				}
			}
			writer.print(message);
		} catch (IOException e) {
			logger.error(e.getMessage());
		}
		return (SKIP_BODY);
	}
	
	private boolean isAuthorizedRemove(){
		try {
			WikiManager manager = (WikiManager) ApplicationManager.appContext.getBean("wikiManager");
			return manager.isAuthorizedRemove(getUserid());
		} catch (Exception e) {
			logger.debug(e);
		}
		return false;
	}
	private boolean isAuthorizedCreate(){
		try {
			WikiManager manager = (WikiManager) ApplicationManager.appContext.getBean("wikiManager");
			return manager.isAuthorizedCreate(getUserid());
		} catch (Exception e) {
			logger.debug(e);
		}
		return false;
	}
	private boolean isAuthorizedUpdate(){
		try {
			WikiManager manager = (WikiManager) ApplicationManager.appContext.getBean("wikiManager");
			return manager.isAuthorizedUpdate(getUserid());
		} catch (Exception e) {
			logger.debug(e);
		}
		return false;
	}
	private boolean isAuthorizedCommend(){
		try {
			WikiManager manager = (WikiManager) ApplicationManager.appContext.getBean("wikiManager");
			return manager.isAuthorizedCommend(getUserid());
		} catch (Exception e) {
			logger.debug(e);
		}
		return false;
	}
	
	private Long getUserid(){
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		HttpSession session = request.getSession();
		Long userid = (Long) session.getAttribute(Constants.SESSION_USER_ID_KEY);
		return userid;
	}
	
	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	
}